package cn.codeforfun.gitee.api.email;

import cn.codeforfun.gitee.api.GiteeService;
import cn.codeforfun.gitee.bean.GiteeEmail;
import cn.codeforfun.gitee.bean.GiteeToken;
import cn.codeforfun.gitee.utils.JsonUtils;
import cn.codeforfun.gitee.utils.OkHttpUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;

import static cn.codeforfun.gitee.constant.GiteeConstant.URL_PREFIX_API;

/**
 * @author wangbin
 */
@RequiredArgsConstructor
@Slf4j
public class GiteeEmailServiceImpl implements GiteeEmailService {
    private final GiteeService giteeService;

    @Override
    public List<GiteeEmail> listEmails() throws IOException {
        GiteeToken token = giteeService.getToken();
        String url = URL_PREFIX_API + "/emails?access_token=" + token.getAccessToken();
        String result = OkHttpUtils.get(url);
        log.debug("getEmails result:{}", result);
        return JsonUtils.toBeanList(result, GiteeEmail.class);
    }
}
